package com.cy.detectsys.controller.mission;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.detectsys.config.CustomException;
import com.cy.detectsys.entity.DetectMission;
import com.cy.detectsys.entity.DetectMissionTarget;
import com.cy.detectsys.entity.DetectTarget;
import com.cy.detectsys.mapper.DbWikiMapper;
import com.cy.detectsys.mapper.DetectMissionTargetMapper;
import com.cy.detectsys.model.PageDto;
import com.cy.detectsys.model.PageReqModel;
import com.cy.detectsys.model.req.DetectMissionReq;
import com.cy.detectsys.model.req.DetectTargetReq;
import com.cy.detectsys.model.vo.DetectMissionVO;
import com.cy.detectsys.service.DetectMissionService;
import com.cy.detectsys.service.DetectMissionTargetService;
import com.cy.detectsys.service.DetectTargetIpService;
import com.cy.detectsys.service.DetectTargetService;
import com.cy.detectsys.util.R;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 【作战任务】 页面相关接口
 */
@RestController
@RequestMapping("mission")
public class MissionController {


    @Resource
    private DetectMissionService missionService;

    @Resource
    private DetectMissionTargetMapper detectMissionTargetMapper;

    @Resource
    private DetectMissionTargetService missionTargetService;

    @Resource
    private DetectTargetService detectTargetService;


    /**
     * 作战任务 查询任务list
     */
    @RequestMapping("list")
    public R getMissionList(@RequestBody PageReqModel<DetectMissionReq> missionReq) {

        PageHelper.startPage(missionReq.getPage(), missionReq.getPageSize());
        QueryWrapper<DetectMission> queryWrapper = new QueryWrapper<>();

        DetectMissionReq model = missionReq.getModel();

        if (model != null) {
            String missionDescribe = model.getMissionDescribe();
            String missionName = model.getMissionName();
            if (StrUtil.isNotEmpty(missionName)) {
                queryWrapper.like("ky_mission_name", missionName);
            }
            if (StrUtil.isNotEmpty(missionDescribe)) {
                queryWrapper.like("ky_mission_brief", missionDescribe);
            }
        }
        queryWrapper.orderByDesc("create_time");

        List<DetectMission> missionList = missionService.list(queryWrapper);


        PageDto<DetectMissionVO> detectMissionVO = PageDto.listToPageVO(missionList, DetectMissionVO.class);

        for (DetectMissionVO detectMission : detectMissionVO.getData()) {
            Integer count = detectMissionTargetMapper.getTargetCountByMissionID(detectMission.getId());
            detectMission.setTargetCount(count); // 将任务关联的目标数量填入
        }

        return R.ok(detectMissionVO);


    }


    @RequestMapping("update")
    public R update(@RequestBody DetectMission missionReq) {
        missionReq.setUpdateTime(new Date());
        missionService.updateById(missionReq);
        return R.ok();
    }

    @RequestMapping("delete")
    public R delete(@RequestBody DetectMission missionReq) {
        missionService.removeById(missionReq);
        return R.ok();
    }


    @RequestMapping("add")
    public R add(@RequestBody DetectMission missionReq) {
        missionReq.setCreateTime(new Date());
        missionService.save(missionReq);
        return R.ok();
    }

    @Resource
    private DbWikiMapper dbWikiMapper;
    @Resource
    private DetectTargetIpService detectTargetIpService;


    /**
     * 作战任务查询任务下目标
     */
    @RequestMapping("target/list")
    public R getTargetList(@RequestBody PageReqModel<DetectTargetReq> missionReq) {
        DetectTargetReq model = missionReq.getModel();
        String missionId = null;
        if (model != null) {
            missionId = model.getMissionId();
            if (StrUtil.isEmpty(missionId)) {
                throw new CustomException("任务ID 未提交无法查询");
            }
        }

        PageHelper.startPage(missionReq.getPage(), missionReq.getPageSize());
        List<DetectTarget> list = detectMissionTargetMapper.getTargetIdByMissionID(missionId);

        PageDto<DetectTargetVO> detectTargetVOPageDto = PageDto.listToPageVO(list, DetectTargetVO.class);

        for (DetectTargetVO detectTarget : detectTargetVOPageDto.getData()) {
            String domain = detectTarget.getKyNetTargetDomain();
            String kyNetTargetEnglishName = detectTarget.getKyNetTargetEnglishName();
            if (StrUtil.isNotEmpty(kyNetTargetEnglishName)) {


                String targetNameEn = detectTarget.getKyNetTargetEnglishName();
                HashMap<String, List<String>> targetIP = detectTargetIpService.selectByTargetName(targetNameEn);

                List<String> domains = targetIP.get("domains");

                List<String> ips = targetIP.get("ips");

                detectTarget.setDomains(domains);
                detectTarget.setIps(ips);


            }

            if (StrUtil.isNotEmpty(domain)) {
                Boolean flag = dbWikiMapper.selectDomian(domain);
                detectTarget.setHaveWiki(flag);
            } else {
                detectTarget.setHaveWiki(false);
            }
        }

        return R.ok(detectTargetVOPageDto);
    }


    @RequestMapping("add/target")
    public R addMissionTarget(@RequestBody DetectMissionTarget missionTarget) {
        if (StrUtil.isNotEmpty(missionTarget.getKyMissionId()) && StrUtil.isNotEmpty(missionTarget.getKyNetTargetId())) {
            missionTarget.setDeleted(0);
            return missionTargetService.save(missionTarget) ? R.ok() : R.error("添加失败");
        }
        return R.error("参数不全");
    }

}
